package com.isias.system.handler;

import com.isias.common.result.Result;
import com.isias.common.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    /**
     * 全局异常
     * */
    @ExceptionHandler(Exception.class)
    public Result AllExceprion(Exception e){
        log.error("出现全局异常"+e);
        e.printStackTrace();
        return Result.fail();
    }
    /***
     * 节点删除异常
     */
    @ExceptionHandler(NodeException.class)
    @ResponseBody
    public Result NodeExpection(NodeException e){
        log.warn("节点删除失败"+e);
//        e.printStackTrace();
        return Result.fail().message(e.getMsg()).code(e.getCode());
    }

    /***
     * 登录异常
     */
    @ExceptionHandler(LoginException.class)
    @ResponseBody
    public Result LoginException(LoginException e){
        log.warn("有用户登录出现问题"+e);
//        e.printStackTrace();
        return Result.fail().message(e.getMsg()).code(e.getCode());
    }
    /**
     * spring security异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.build(null, ResultCodeEnum.PERMISSION);
    }

    /***
     * 注册异常
     */
    @ExceptionHandler(GeneralException.class)
    @ResponseBody
    public Result registerException(GeneralException e){
        log.warn("有用户重复注册"+e);
//        e.printStackTrace();
        return Result.fail().message(e.getMsg()).code(e.getCode());
    }

    /***
     * 创建试卷异常
     */
    @ExceptionHandler(CreateExamException.class)
    @ResponseBody
    public Result CreatExamException(GeneralException e){
        log.error("为用户创建试卷异常:"+e);
//        e.printStackTrace();
        return Result.fail().message("创建试卷异常").code(603);
    }

}
